/**
 * https://leetcode.cn/submissions/detail/564465631/
 * 875. 爱吃香蕉的珂珂
 * Medium, 黄伟杰 2024.9.13
 * 二分查找
 */

#include <bits/stdc++.h>
using namespace std;

class Solution
{
    long gettime(vector<int> &piles, int speed)
    {
        long time = 0;
        for (int i : piles)
        {
            int cut = (i + speed - 1) / speed;
            time += cut;
        }
        return time;
    }

public:
    int minEatingSpeed(vector<int> &piles, int h)
    {
        sort(piles.begin(), piles.end());
        int left = 1, right = 10e8 + 5;
        while (right > left)
        {
            long mid = (right + left) / 2;
            if (gettime(piles, mid) > h)
                left = mid + 1;
            else
                right = mid;
        }
        return left;
    }
};